
#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4;      
sfrb ADCSRA=6;
sfrb ADMUX=7;
sfrb ACSR=8;
sfrb UBRRL=9;
sfrb UCSRB=0xa;
sfrb UCSRA=0xb;
sfrb UDR=0xc;
sfrb SPCR=0xd;
sfrb SPSR=0xe;
sfrb SPDR=0xf;
sfrb PIND=0x10;
sfrb DDRD=0x11;
sfrb PORTD=0x12;
sfrb PINC=0x13;
sfrb DDRC=0x14;
sfrb PORTC=0x15;
sfrb PINB=0x16;
sfrb DDRB=0x17;
sfrb PORTB=0x18;
sfrb EECR=0x1c;
sfrb EEDR=0x1d;
sfrb EEARL=0x1e;
sfrb EEARH=0x1f;
sfrw EEAR=0x1e;   
sfrb UBRRH=0x20;
sfrb UCSRC=0X20;
sfrb WDTCR=0x21;
sfrb ASSR=0x22;
sfrb OCR2=0x23;
sfrb TCNT2=0x24;
sfrb TCCR2=0x25;
sfrb ICR1L=0x26;
sfrb ICR1H=0x27;
sfrw ICR1=0x26;   
sfrb OCR1BL=0x28;
sfrb OCR1BH=0x29;
sfrw OCR1B=0x28;  
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a;  
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c;  
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=0x31;
sfrb TCNT0=0x32;
sfrb TCCR0=0x33;
sfrb MCUCSR=0x34;
sfrb MCUCR=0x35;
sfrb TWCR=0x36;
sfrb SPMCR=0x37;
sfrb TIFR=0x38;
sfrb TIMSK=0x39;
sfrb GIFR=0x3a;
sfrb GICR=0x3b;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x80
	.EQU __sm_mask=0x70
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0x60
	.EQU __sm_ext_standby=0x70
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#endasm

#pragma used+

unsigned char cabs(signed char x);
unsigned int abs(int x);
unsigned long labs(long x);
float fabs(float x);
int atoi(char *str);
long int atol(char *str);
float atof(char *str);
void itoa(int n,char *str);
void ltoa(long int n,char *str);
void ftoa(float n,unsigned char decimals,char *str);
void ftoe(float n,unsigned char decimals,char *str);
void srand(int seed);
int rand(void);
void *malloc(unsigned int size);
void *calloc(unsigned int num, unsigned int size);
void *realloc(void *ptr, unsigned int size); 
void free(void *ptr);

#pragma used-
#pragma library stdlib.lib

#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-

eeprom unsigned char reserver[10];
unsigned char reserve;

eeprom unsigned int VIDEORAM_R_eep[48]; 
eeprom unsigned int VIDEORAM_G_eep[48];

unsigned char TEST_flags = 0;

unsigned int VIDEORAM_R[48];
unsigned int VIDEORAM_G[48]; 
unsigned int VIDEORAM_RANDOM[48];
unsigned int VIDEORAM_OUT[48];
unsigned char VIDEO_status = 1;
unsigned char VIDEO_intindex;
unsigned char VIDEO_bitindex;
unsigned char VIDEO_phase = 0;
unsigned char VIDEO_flags = 0;
unsigned int VIDEO_delay;
unsigned int VIDEO_int1; 
unsigned int VIDEO_int2; 
unsigned int VIDEO_int3; 
unsigned int VIDEO_int1_OUT;
unsigned int VIDEO_int2_OUT;
unsigned int VIDEO_int3_OUT;

unsigned char TmCounter = 0;

interrupt [10] void timer0_ovf_isr(void)
{
TIMSK=0x00;             
#asm("sei");            

switch (VIDEO_status) { 
case 1: 
PORTD.5=0;
PORTD.6=1;
PORTB.2=0;
PORTB.3=1;
PORTC.2=0;
PORTC.3=1;

VIDEO_bitindex = 16;
VIDEO_intindex = 0;
VIDEO_flags |= 0x20;
VIDEO_status = 2;
break;
case 2: 
#asm("wdr");

if (VIDEO_bitindex == 16) {
if (VIDEO_intindex > 0) {
VIDEORAM_OUT[VIDEO_intindex-1] = VIDEO_int1_OUT;
VIDEORAM_OUT[VIDEO_intindex+15] = VIDEO_int2_OUT;
VIDEORAM_OUT[VIDEO_intindex+31] = VIDEO_int3_OUT;
}
if (VIDEO_intindex == 16) {

VIDEO_status = 10;
break;
}        
if (VIDEO_flags & 0x10) {
VIDEO_int1 = VIDEORAM_RANDOM[VIDEO_intindex];
VIDEO_int2 = VIDEORAM_RANDOM[VIDEO_intindex+16];
VIDEO_int3 = VIDEORAM_RANDOM[VIDEO_intindex+32];
} else {
if (VIDEO_phase) {
VIDEO_int1 = VIDEORAM_R[VIDEO_intindex];
VIDEO_int2 = VIDEORAM_R[VIDEO_intindex+16];
VIDEO_int3 = VIDEORAM_R[VIDEO_intindex+32];
} else {
VIDEO_int1 = VIDEORAM_G[VIDEO_intindex];
VIDEO_int2 = VIDEORAM_G[VIDEO_intindex+16];
VIDEO_int3 = VIDEORAM_G[VIDEO_intindex+32];
}       
}
VIDEO_bitindex = 0;
VIDEO_intindex++;    
}

if (VIDEO_flags & 0x08) {
PORTD.7 = 0; 
PORTB.4 = 0; 
PORTC.4 = 0; 
} else if (VIDEO_flags & 0x04) {
PORTD.7 = 1; 
PORTB.4 = 1; 
PORTC.4 = 1; 
} else {
if (VIDEO_int1 & 0b1000000000000000) PORTD.7 = 1; else PORTD.7 = 0;
if (VIDEO_int2 & 0b1000000000000000) PORTB.4 = 1; else PORTB.4 = 0;
if (VIDEO_int3 & 0b1000000000000000) PORTC.4 = 1; else PORTC.4 = 0;
}

PORTD.6 = 0;
PORTB.3 = 0;
PORTC.3 = 0;

VIDEO_int1 <<= 1;
VIDEO_int2 <<= 1;
VIDEO_int3 <<= 1;

VIDEO_bitindex++;  

VIDEO_int1_OUT <<= 1;            
if (PINB.0 == 1) VIDEO_int1_OUT |= 1;   
VIDEO_int2_OUT <<= 1;            
if (PINC.0 == 1) VIDEO_int2_OUT |= 1;   
VIDEO_int3_OUT <<= 1;            
if (PINC.5 == 1) VIDEO_int3_OUT |= 1;   

PORTD.6 = 1;
PORTB.3 = 1;
PORTC.3 = 1;

VIDEO_status = 2;        
break;
case 10: 
PORTD.2 = 0;
PORTD.3 = 0;

PORTD.5 = 1;
PORTB.2 = 1;
PORTC.2 = 1;

VIDEO_delay = 0;
VIDEO_status = 43;

PORTD.5 = 0;
PORTB.2 = 0;
PORTC.2 = 0;

TmCounter = 0;

TCCR0=0x02;
break;

case 43:
if (VIDEO_delay++ >= 25) VIDEO_status = 51;
#asm("wdr");
break;

case 51: 
VIDEO_flags |= 0x40;

if (VIDEO_flags & 0x01) {
PORTD.2 = 1;
}
if (VIDEO_flags & 0x02) {
PORTD.3 = 1;            
} 
if ((VIDEO_flags & (0x02 | 0x01)) == 0) { 
if (!(VIDEO_phase))     PORTD.2 = 1;
if (VIDEO_phase)        PORTD.3 = 1;
}

VIDEO_phase = ~VIDEO_phase;

TmCounter = 0xEA;

TCCR0=0x01;

VIDEO_status = 1;    
break;
}

TCNT0 = TmCounter;      
TIFR |= 0b00000001;     
TIMSK = 0x01;           
}

void reset_DMs() {

delay_ms(1);
PORTD.6=1;
PORTB.3=1;
PORTC.3=1;
delay_ms(1);
PORTD.5=1;    
PORTB.2=1;    
PORTC.2=1;    
delay_ms(1);
PORTD.5=0;    
PORTB.2=0;    
PORTC.2=0;    
delay_ms(1);
PORTD.5=1;    
PORTB.2=1;    
PORTC.2=1;    
delay_ms(1);
PORTD.5=0;    
PORTB.2=0;    
PORTC.2=0;    
delay_ms(1);
PORTD.5=1;    
PORTB.2=1;    
PORTC.2=1;    
delay_ms(1);
PORTD.5=0;    
PORTB.2=0;    
PORTC.2=0;    
delay_ms(1);
PORTD.5=1;    
PORTB.2=1;    
PORTC.2=1;    
delay_ms(1);
PORTD.6=0;
PORTB.3=0;
PORTC.3=0;
delay_ms(1);
PORTD.6=1;
PORTB.3=1;
PORTC.3=1;
delay_ms(1);
PORTD.6=0;
PORTB.3=0;
PORTC.3=0;
delay_ms(1);
PORTD.6=1;
PORTB.3=1;
PORTC.3=1;
delay_ms(1);
PORTD.6=0;
PORTB.3=0;
PORTC.3=0;
delay_ms(1);
PORTD.6=1;
PORTB.3=1;
PORTC.3=1;
delay_ms(1);
PORTD.6=0;
PORTB.3=0;
PORTC.3=0;
delay_ms(1);
PORTD.5=0;    
PORTB.2=0;    
PORTC.2=0;    

}

unsigned char buffer1[128 ]; 
unsigned char buffer1_start=0;
unsigned char buffer1_size=0;

interrupt [14] void usart_tx_isr(void)
{
if (buffer1_size>0){
#asm("wdr");
UDR = buffer1[buffer1_start++];
buffer1_start&=(128 -1);
buffer1_size--;
}
}

void put_data_to_buffer1(unsigned char data)
{
if (!buffer1_size && (UCSRA & (unsigned char)(1 << 5))) 
UDR = data;
else
if (buffer1_size<128 ) 
buffer1[(buffer1_start+buffer1_size++)&(128 -1)] = data;
}

void put_string_to_buffer1_flash(flash unsigned char * data)
{
flash unsigned char * d;
for(d=data;*d;d++) 
put_data_to_buffer1(*d);
}
void put_string_to_buffer1(unsigned char * data)
{
unsigned char * d;
for(d=data;*d;d++) 
put_data_to_buffer1(*d);
}

unsigned char BUFFER_mode = 0;
unsigned char BUFFER_curlength;
unsigned char BUFFER_LEDnum[3];
unsigned char BUFFER_LEDnum_int; 
unsigned char BUFFER_Red[4];
unsigned int  BUFFER_Red_int; 
unsigned char BUFFER_Green[4];
unsigned int  BUFFER_Green_int;
unsigned char BUFFER_temp[48]; 

interrupt [12] void usart_rx_isr(void)
{
unsigned char tmp0;

tmp0 = UDR; 
#asm("wdr");

put_data_to_buffer1(tmp0);

switch (BUFFER_mode) {
case 0x00 :
if (tmp0 >= '0' && tmp0 <= '9') {
BUFFER_curlength = 1;
BUFFER_LEDnum[0] = tmp0;
BUFFER_mode = 0x10;
} else if (tmp0 == 'T' || tmp0 == 't') {    
BUFFER_curlength = 1;
BUFFER_mode = 0x20;
} else if (tmp0 == 'S' || tmp0 == 's') {    
BUFFER_curlength = 1;
BUFFER_mode = 0x30;
} else if (tmp0 == 'V' || tmp0 == 'v') {    
BUFFER_curlength = 1;
BUFFER_mode = 0x40;
} else if (tmp0 == 'R' || tmp0 == 'r') {    
BUFFER_curlength = 1;
BUFFER_mode = 0x50;
} else if (tmp0 == 'C' || tmp0 == 'c') {    
BUFFER_curlength = 1;
BUFFER_mode = 0x60;
} else if (!(tmp0==' ' || tmp0==9 || tmp0==13 || tmp0==10)) {
BUFFER_mode = 0x01;
goto err;
}  
break;          

case 0x10:
if (tmp0 == ':') {
BUFFER_LEDnum[BUFFER_curlength] = 0;
BUFFER_LEDnum_int = atoi(BUFFER_LEDnum);
if (BUFFER_LEDnum_int == 0) {
BUFFER_mode = 0x01;    
goto err;
} else {                             
BUFFER_mode = 0x11;
BUFFER_curlength = 0;
}
} else if (tmp0 >= '0' && tmp0 <= '9') {
if (BUFFER_curlength == 2) {
BUFFER_mode = 0x01;    
goto err;
} else {
BUFFER_LEDnum[BUFFER_curlength] = tmp0;
BUFFER_curlength++;
}
} else {
BUFFER_mode = 0x01;        
goto err;
}
break;

case 0x11:
if (tmp0 == ',') {
BUFFER_Red[BUFFER_curlength] = 0;

BUFFER_Red_int = atoi(BUFFER_Red);
if (BUFFER_curlength == 0) {
BUFFER_mode = 0x01;    
goto err;
} else {                             
BUFFER_mode = 0x12;
BUFFER_curlength = 0;
}
} else if (tmp0 >= '0' && tmp0 <= '9') {
if (BUFFER_curlength == 3) {
BUFFER_mode = 0x01;    
goto err;
} else {
BUFFER_Red[BUFFER_curlength] = tmp0;
BUFFER_curlength++;
}
} else if (!(BUFFER_curlength==0 && (tmp0==' ' || tmp0==9)) ) {      
BUFFER_mode = 0x01;        
goto err;
}
break;

case 0x12:
if (tmp0 == 13 || tmp0 == 10) {
BUFFER_Green[BUFFER_curlength] = 0;

BUFFER_Green_int = atoi(BUFFER_Green);
if (BUFFER_curlength == 0) {
BUFFER_mode = 0x01;    
goto err;
} else {
if (BUFFER_LEDnum_int > 0 && BUFFER_LEDnum_int <= 48) {
VIDEORAM_R[BUFFER_LEDnum_int-1] = (BUFFER_Red_int << 8);                                                      
VIDEORAM_G[BUFFER_LEDnum_int-1] = (BUFFER_Green_int << 8);                                                      
BUFFER_mode = 0x00 ;
put_string_to_buffer1_flash("\r\nOK\r\n");
} else {
BUFFER_mode = 0x01;                    
goto err;
}
}
} else if (tmp0 >= '0' && tmp0 <= '9') {
if (BUFFER_curlength == 3) {
BUFFER_mode = 0x01;    
goto err;
} else {
BUFFER_Green[BUFFER_curlength] = tmp0;
BUFFER_curlength++;
}
} else if (!(BUFFER_curlength==0 && (tmp0==' ' || tmp0==9)) ) {      
BUFFER_mode = 0x01;        
goto err;
}
break;

case 0x20:
switch (BUFFER_curlength) {
case 1: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 2: if (tmp0 != 'S' && tmp0 != 's') BUFFER_mode = 0x01; break;
case 3: if (tmp0 != 'T' && tmp0 != 't') BUFFER_mode = 0x01; break;
case 4: 
if (tmp0 == '1') {

TEST_flags = 0x01;
} else if (tmp0 == '2') {

TEST_flags = 0x02;
} else {
TEST_flags = 0;                        
BUFFER_mode = 0x01;
}
break;
case 5:
if (tmp0 == 13 || tmp0 == 10) {
put_string_to_buffer1_flash("\r\nTEST MODE OK\r\n");
TEST_flags |= 0x10;
BUFFER_mode = 0x00 ;
} else if (!(tmp0==' ' || tmp0==9)) {
TEST_flags = 0;                        
BUFFER_mode = 0x01;
}
break;
} 
if (!(tmp0==' ' || tmp0==9)) BUFFER_curlength++;
if (BUFFER_mode == 0x01) goto err;
break;

case 0x30:
switch (BUFFER_curlength) {
case 1: if (tmp0 != 'T' && tmp0 != 't') BUFFER_mode = 0x01; break;
case 2: if (tmp0 != 'A' && tmp0 != 'a') BUFFER_mode = 0x01; break;
case 3: if (tmp0 != 'T' && tmp0 != 't') BUFFER_mode = 0x01; break;
case 4: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 5:
if (tmp0 == 13 || tmp0 == 10) {
put_string_to_buffer1_flash("\r\nSTATE MODE OK\r\n");
TEST_flags |= 0x20;
BUFFER_mode = 0x00 ;
} else if (!(tmp0==' ' || tmp0==9)) {
BUFFER_mode = 0x01;
}
break;
}
if (!(tmp0==' ' || tmp0==9)) BUFFER_curlength++;
if (BUFFER_mode == 0x01) goto err;
break;

case 0x40:
switch (BUFFER_curlength) {
case 1: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 2: if (tmp0 != 'R' && tmp0 != 'r') BUFFER_mode = 0x01; break;
case 3:
if (tmp0 == 13 || tmp0 == 10) {
put_string_to_buffer1_flash("\r\nStatusQuo Device\r\nNEC-HITACHI Corp.\r\nhttp://coolish.ru\r\nMADE IN CHINA\r\n\r\nFirmware: ");
put_string_to_buffer1_flash("Jul 22 2010");
put_data_to_buffer1(' ');
put_string_to_buffer1_flash("00:49:23");
put_string_to_buffer1_flash("\r\n");
BUFFER_mode = 0x00 ;
} else if (!(tmp0==' ' || tmp0==9)) {
BUFFER_mode = 0x01;
}
break;
}
if (!(tmp0==' ' || tmp0==9)) BUFFER_curlength++;
if (BUFFER_mode == 0x01) goto err;
break;

case 0x50:
switch (BUFFER_curlength) {
case 1: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 2: if (tmp0 != 'S' && tmp0 != 's') BUFFER_mode = 0x01; break;
case 3: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 4: if (tmp0 != 'T' && tmp0 != 't') BUFFER_mode = 0x01; break;
case 5:
if (tmp0 == 13 || tmp0 == 10) {

#asm("RJMP 0x1C00");

} else if (!(tmp0==' ' || tmp0==9)) {
BUFFER_mode = 0x01;
}
break;
}
if (!(tmp0==' ' || tmp0==9)) BUFFER_curlength++;
if (BUFFER_mode == 0x01) goto err;
break;

case 0x60:
switch (BUFFER_curlength) {
case 1: if (tmp0 != 'L' && tmp0 != 'l') BUFFER_mode = 0x01; break;
case 2: if (tmp0 != 'E' && tmp0 != 'e') BUFFER_mode = 0x01; break;
case 3: if (tmp0 != 'A' && tmp0 != 'a') BUFFER_mode = 0x01; break;
case 4: if (tmp0 != 'R' && tmp0 != 'r') BUFFER_mode = 0x01; break;
case 5:
if (tmp0 == 13 || tmp0 == 10) {
put_string_to_buffer1_flash("\r\nCLEAR MODE OK\r\n");
TEST_flags |= 0x40;
BUFFER_mode = 0x00 ;
} else if (!(tmp0==' ' || tmp0==9)) {
BUFFER_mode = 0x01;
}
break;
}
if (!(tmp0==' ' || tmp0==9)) BUFFER_curlength++;
if (BUFFER_mode == 0x01) goto err;
break;

case 0x01: err:
if (tmp0 == 13 || tmp0 == 10) {        
BUFFER_mode = 0x00 ;
put_string_to_buffer1_flash("\r\nERROR\r\n");
}
break;       
}    
}

void main(void)
{
unsigned int i;    
unsigned char temp_string[10]; 

#asm("wdr");
WDTCR = 0b00011000;
WDTCR = 0b00001011;     

PORTB=0x00;
DDRB=0x00;

PORTC=0x00;
DDRC=0x00;

PORTD=0x00;
DDRD=0x00;

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

MCUCR=0x00;

ACSR=0x80;
SFIOR=0x00;

reserve = reserver[0];
{ 
for (i = 0; i < 48; i++) { 
#asm("wdr");
VIDEORAM_R[i] = VIDEORAM_R_eep[i]; 
VIDEORAM_G[i] = VIDEORAM_G_eep[i]; 
}
}

UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;

TCCR0=0x01;
TCNT0=0x00;

TIMSK=0x01;

PORTD.2=0;
DDRD.2=1;
PORTD.3=0;
DDRD.3=1;
PORTD.4=0;
DDRD.4=0;

PORTD.7=0;
DDRD.7=1;
PORTD.6=0;
DDRD.6=1;
PORTD.5=0;
DDRD.5=1;
PORTB.0=0;
DDRB.0=0;

PORTB.4=0;
DDRB.4=1;
PORTB.3=0;
DDRB.3=1;
PORTB.2=0;
DDRB.2=1;
PORTC.0=0;
DDRC.0=0;

PORTC.4=0;
DDRC.4=1;
PORTC.3=0;
DDRC.3=1;
PORTC.2=0;
DDRC.2=1;
PORTC.5=0;
DDRC.5=0;

#asm("wdr");

reset_DMs();

#asm("wdr");

#asm("sei");

while (1) {
#asm("wdr");

if (PIND.4 == 0) { 

#asm("cli");
PORTD.3 = PORTD.2 = 0;
VIDEO_flags |= 0x08; 
#asm("sei");    

{  
for (i = 0; i < 48; i++) { 
#asm("wdr");
VIDEORAM_R_eep[i] = VIDEORAM_R[i]; 
VIDEORAM_G_eep[i] = VIDEORAM_G[i]; 
}
} 

#asm("cli");
TIMSK=0x00;             
#asm("sei");    

#asm("cli");
put_string_to_buffer1_flash("\r\nDC POWER LOSS. IONISTOR MODE ON\r\n");
#asm("sei");    

while (1) {

for (i = 0; i < 1000; i++) { 
#asm("wdr");
delay_ms(10); 
}

while (PIND.4 == 0) { 
#asm("wdr");
delay_ms(1);    
}

for (i = 0; i < 1000; i++) { 
#asm("wdr");
delay_ms(10); 
}

if (PIND.4 == 1) break;

}

#asm("cli");    
put_string_to_buffer1_flash("\r\nDC POWER ONLINE\r\n");
#asm("sei");    

#asm("wdr");
reset_DMs();                             
#asm("cli");
VIDEO_flags &= ~0x08;
VIDEO_status = 1; 
TIMSK = 0x01;           
#asm("sei");
}        

if (TEST_flags & 0x10) { 
unsigned int tmp1;
if (TEST_flags & 0x01) {

#asm("cli");
VIDEO_flags |= 0x04 | 0x02;
#asm("sei");

for (i = 0; i < 100; i++) {
#asm("wdr");
delay_ms(10);
}

for (i = 0; i < 48; i++) {
#asm("wdr");

#asm("cli");
tmp1 = VIDEORAM_OUT[i];
#asm("sei");

BUFFER_temp[i] = (unsigned char)(tmp1 & 0x0001); 
}

#asm("cli");
VIDEO_flags &= ~0x02;                
VIDEO_flags |= 0x01;                
#asm("sei");

for (i = 0; i < 100; i++) {
#asm("wdr");
delay_ms(10);
} 

for (i = 0; i < 48; i++) {
#asm("wdr");
itoa(i+1, temp_string);

#asm("cli");    
put_string_to_buffer1(temp_string);                       
put_string_to_buffer1_flash(":");                       
if (BUFFER_temp[i]) {
put_string_to_buffer1_flash("OK");                       
} else {
put_string_to_buffer1_flash("NC");                       
}
put_data_to_buffer1(',');
#asm("sei");    

#asm("cli");
tmp1 = VIDEORAM_OUT[i];
#asm("sei");

#asm("cli");    
if (tmp1 & 0x0001) {
put_string_to_buffer1_flash("OK");                       
} else {
put_string_to_buffer1_flash("NC");                       
}                       
put_string_to_buffer1_flash("\r\n");
#asm("sei");    

delay_ms(20);                       
}

#asm("cli");
VIDEO_flags &= ~(0x04 | 0x01); 
#asm("sei");
}          

if (TEST_flags & 0x02) {

unsigned int good = 0;

for (i = 0; i < 48; i++) {
#asm("wdr");

VIDEORAM_RANDOM[i] = rand();
}

#asm("cli");
VIDEO_flags |= (0x10);
#asm("sei");

for (i = 0; i < 100; i++) {
#asm("wdr");
delay_ms(10);
}

for (i = 0; i < 48; i++) {
#asm("wdr");

#asm("cli");
tmp1 = VIDEORAM_OUT[i];
#asm("sei");

if ( (tmp1 & 0xFFFE) == (VIDEORAM_RANDOM[i] & 0xFFFE) ) good++;
}           

#asm("cli");
VIDEO_flags &= ~(0x10);
#asm("sei");

itoa((100 * good) / 48, temp_string);
#asm("cli");    
put_string_to_buffer1_flash("Data loopback: ");                
put_string_to_buffer1(temp_string);                       
put_string_to_buffer1_flash("% data correct\r\n");                
#asm("sei");    
}          

#asm("cli");
TEST_flags &= ~(0x10 | 0x01 | 0x02);        
#asm("sei");
}

if (TEST_flags & 0x20) {  
unsigned int tmp1;
for (i = 0; i < 48; i++) {
#asm("wdr");

itoa(i+1, temp_string);
#asm("cli");    
put_string_to_buffer1(temp_string);                       
put_string_to_buffer1_flash(":");
#asm("sei");    
tmp1 = (VIDEORAM_R[i] >> 8);                       
itoa(tmp1, temp_string);
#asm("cli");    
put_string_to_buffer1(temp_string);                       
put_data_to_buffer1(',');                       
#asm("sei");    
tmp1 = (VIDEORAM_G[i] >> 8);                       
itoa(tmp1, temp_string);
#asm("cli");    
put_string_to_buffer1(temp_string);                       
put_string_to_buffer1_flash("\r\n");
#asm("sei");    

delay_ms(20);                       
}                                                   

#asm("cli");
TEST_flags &= ~(0x20);        
#asm("sei");
}

if (TEST_flags & 0x40) {  
for (i = 0; i < 48; i++) {
#asm("wdr");

VIDEORAM_R[i] = VIDEORAM_G[i] = 0;     

VIDEORAM_R_eep[i] = 0;
VIDEORAM_G_eep[i] = 0;                    
}                                                   

#asm("cli");
TEST_flags &= ~(0x40);        
#asm("sei");
}

}

}
